package ant.activity;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class AntActiviy extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    }
    
    /**
     * 捕获全局异常的方法,并将异常用intent方式转发到class1中去
     * @param class1 一个activity的class类型参数
     */
    protected void errorHandler(final Class<? extends Activity> class1) {
		Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread thread, Throwable ex) {
				Exception e = new Exception(ex);
				e.printStackTrace();
				final Writer result = new StringWriter();
		        final PrintWriter printWriter = new PrintWriter(result);
		        ex.printStackTrace(printWriter);
		        String stacktrace = result.toString();
		        printWriter.close();
		        System.out.println(stacktrace);
				Intent intent = new Intent(getApplicationContext(), class1);
				intent.putExtra("cause", stacktrace);
				startActivity(intent);
				System.exit(0);
			}
		});
	}
}